﻿/***********************************************************
Copyright © 2015 Howyoung.
************************************************************
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty.
************************************************************
您可以对所收受的本程序源代码，无论以何种媒介，复制与发布其完
整的复制物，然而您必须以显著及适当的方式在每一份复制物上发布
适当的著作权标示及无担保声明。
***********************************************************/
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace UpdateProgram
{
    public partial class Welcome : Form
    {
        int pictureCount = 1;

        public Welcome()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                switch (pictureCount)
                {
                    case 1:
                        pictureBox.Image = Properties.Resources.p1;
                        break;
                    case 2:
                        pictureBox.Image = Properties.Resources.p2;
                        break;
                    case 3:
                        pictureBox.Image = Properties.Resources.p3;
                        break;
                    case 4:
                        pictureBox.Image = Properties.Resources.p4;
                        break;
                    case 5:
                        pictureBox.Image = Properties.Resources.p5;
                        break;
                    case 6:
                        pictureBox.Image = Properties.Resources.p6;
                        break;
                    case 7:
                        pictureBox.Image = Properties.Resources.p7;
                        break;
                    case 8:
                        pictureBox.Image = Properties.Resources.p8;
                        break;
                    case 9:
                        pictureBox.Image = Properties.Resources.p9;
                        break;
                    case 10:
                        pictureBox.Image = Properties.Resources.p10;
                        break;
                    case 11:
                        pictureBox.Image = Properties.Resources.p11;
                        break;
                    case 12:
                        pictureBox.Image = Properties.Resources.p12;
                        break;
                    case 13:
                        pictureBox.Image = Properties.Resources.p13;
                        break;
                    case 14:
                        pictureBox.Image = Properties.Resources.p14;
                        break;
                }
                pictureCount++;
                if (pictureCount > 14)
                {
                    pictureBox.Image = Properties.Resources.p1;
                    pictureCount = 1;
                }
            }
            catch (Exception)
            { }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            //拷贝设置
            try
            {
                string programPath = Convert.ToString(Registry.GetValue("HKEY_CURRENT_USER\\Software\\MinecraftSavesBackup", "ProgramPath", null));
                if (programPath == null)
                {
                    if (MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                    "这是可能有用的信息：" + Environment.NewLine + "我们无法找到旧程序，因此无法帮您保留设置。" + Environment.NewLine + Environment.NewLine +
                    "您可以试试：" + Environment.NewLine + "点击取消，打开主程序并重新检查更新。" + Environment.NewLine +
                    "如问题还未解决，或您打算放弃设置，请单击点击确定。", "出现错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2) != DialogResult.OK)
                    {
                        Application.ExitThread();
                        return;
                    }
                }
                else
                {
                    string programDirectory = new FileInfo(programPath).DirectoryName;
                    if (!programDirectory.EndsWith("\\")) programDirectory += "\\";
                    string settingsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                    if (settingsDirectory.EndsWith("\\"))
                    {
                        settingsDirectory += "MinecraftSavesBackup\\";
                    }
                    else
                    {
                        settingsDirectory += "\\MinecraftSavesBackup\\";
                    }
                    Directory.CreateDirectory(settingsDirectory);
                    string[] settingsFileNames = new string[] { "BackupFolder.hsxml", "BackupPath.ini", "Config.hsxml", "MD5.hsxml", "savesPath.ini" };
                    foreach (string settingsFileName in settingsFileNames)
                    {

                        if (File.Exists(settingsDirectory + settingsFileName)) File.Delete(settingsDirectory + settingsFileName);
                        if (File.Exists(programDirectory + settingsFileName)) File.Copy(programDirectory + settingsFileName, settingsDirectory + settingsFileName);
                    }
                }
            }
            catch (Exception ex)
            {
                if (MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                    "这是可能有用的信息：" + Environment.NewLine + "我们无法帮您保留设置，" + ex.Message + Environment.NewLine + Environment.NewLine +
                    "您可以试试：" + Environment.NewLine + "点击取消，打开主程序并重新检查更新。" + Environment.NewLine +
                    "如问题还未解决，或您打算放弃设置，请单击点击确定。", "出现错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2) != DialogResult.OK)
                {
                    Application.ExitThread();
                    return;
                }
            }
            //定位并打开卸载程序
            try
            {
                string uninstallString = null;
                RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
                foreach (string keyName in uninstallKey.GetSubKeyNames())
                {
                    RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                    if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft存档备份")
                    {
                        uninstallString = Convert.ToString(appKey.GetValue("UninstallString"));
                    }
                }
                if (uninstallString == null)
                {
                    if (MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                        "这是可能有用的信息：" + Environment.NewLine + "我们不知道该如何卸载旧程序。" + Environment.NewLine + Environment.NewLine +
                        "您可以试试：" + Environment.NewLine + "转到控制面板\\程序\\程序和功能，卸载Minecraft存档备份应用，并点击确定。" + Environment.NewLine +
                        "如果无法找到，说明原程序不存在，请直接点击确定。", "出现错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                    {
                        Setup Setup = new Setup();
                        Setup.Show();
                        Hide();
                    }
                }
                else
                {
                    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/c " + uninstallString);
                    processStartInfo.UseShellExecute = false;
                    processStartInfo.CreateNoWindow = true;
                    Process.Start(processStartInfo);
                    Uninstall Uninstall = new Uninstall();
                    Uninstall.Show();
                    Hide();
                }
            }
            catch (Exception ex)
            {
                if (MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                        "这是可能有用的信息：" + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                        "您可以试试：" + Environment.NewLine + "转到控制面板\\程序\\程序和功能，卸载Minecraft存档备份应用，并点击确定。" + Environment.NewLine +
                        "如果无法找到，说明原程序不存在，请直接点击确定。", "出现错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                {
                    Setup Setup = new Setup();
                    Hide();
                }
            }
        }

        private void Welcome_Load(object sender, EventArgs e)
        {
            if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "setup.exe"))
            {
                if (MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                "这是可能有用的信息：" + Environment.NewLine + "我们找不到要安装的新程序了。" + Environment.NewLine + Environment.NewLine +
                "您可以试试：" + Environment.NewLine + "打开主程序并重新下载更新。" + Environment.NewLine +
                "如问题还未解决，请手动卸载旧版本并下载新版本。要我们为您转到下载页么？", "出现错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    Process.Start("https://minecraftsavesbackup.codeplex.com/releases");
                }
                Application.ExitThread();
            }
        }
    }
}
